﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using CQLQueries.Analyzers;

namespace CQLQueries
{
    public class QueryParametr
    {
        public String Str;
        public String[] SubStrs;
        public Type[] SubStrsTypes;
        public Object Val;

        public Type EndType
        {
            get
            {
                return SubStrsTypes[SubStrsTypes.Length-1];
            }
        }

        public int Get(Type owner, String str1)
        {   
                        
            if (owner == null)
            {
                Str = str1;
                if (str1.Length == 0) return -3;
                SubStrs = str1.Split('.');
                SubStrsTypes = new Type[SubStrs.Length];
                if (!CQL_Semantic.Instance.psevdonims.ContainsKey(SubStrs[0]))
                {
                    return -2;
                }
                String SelectText = CQL_Semantic.Instance.psevdonims[SubStrs[0]];
                System.Type[] ExistingTypes = Assembly.Load("daSyntaxAnalyser").GetTypes();
                foreach (Type t in ExistingTypes)
                {
                    if (t.Name == SelectText)
                    {
                        SubStrsTypes[0] = t;
                        if (SubStrs.Length == 0)
                        {                            
                            return 0;
                        }
                        else
                        {
                            String temp = "";
                            for (int i = 1; i < SubStrs.Length; ++i) temp = temp + SubStrs[i];
                            return Get(t, temp);
                        }                        
                    }
                }
                return -1;
            }
            else
            {
                Type type = owner;                
                int count;
                if (SubStrsTypes == null)
                {
                    SubStrs = str1.Split('.');
                    SubStrsTypes = new Type[SubStrs.Length];
                    count = 0;
                }
                else count = 1;
                for(int i=count; i<SubStrs.Length;++i)
                {
                    String s = SubStrs[i];
                    int f = s.IndexOf('[');
                    String index_val;
                    bool ismass = false;
                    String s2 = s;
                    if (f != -1)
                    {
                        ismass = true;
                        s2 = s.Substring(0, f);
                        index_val = s.Substring(f, s.Length - f - 2);
                    }

                    System.Reflection.MemberInfo[] Members = type.GetMembers();
                    bool b = false;
                    foreach (MemberInfo m in Members)
                    {
                        if (m.Name == s2)
                        {
                            b = true;
                            if (m.MemberType == MemberTypes.Field)
                            {
                                type = ((FieldInfo)m).FieldType;
                            }
                            else if (m.MemberType == MemberTypes.Property)
                            {
                                type = ((PropertyInfo)m).PropertyType;
                                SubStrsTypes[count] = type;
                                if (ismass)
                                {
                                    int int1 = type.FullName.IndexOf('[');
                                    int1 = type.FullName.IndexOf('.', int1);
                                    int ind2 = type.FullName.IndexOf(',', int1);
                                    String mass_member = type.FullName.Substring(int1 + 1, ind2 - int1 - 1);
                                    Type[] ts = Assembly.Load("daSyntaxAnalyser").GetTypes();
                                    foreach (Type tt in ts)
                                    {
                                        if (tt.Name == mass_member)
                                        {
                                            SubStrsTypes[count] = tt;
                                            type = tt;
                                            break;
                                        }
                                    }

                                }
                            }
                            else if (m.MemberType == MemberTypes.Method)
                            {
                                type = ((MethodInfo)m).ReturnType;
                            }
                            else type = m.GetType();
                            if (!ismass) SubStrsTypes[count] = type;
                            count++;
                            break;
                        }
                    }
                    if (!b)
                    {
                        return -4;
                    }
                }
            }
            return 0;
        }
    }
}
